/**
 * 用户中心-我的封装
 * @author hongwei 2018-09-19
 */
(function (window) {
    'use strict';
    var userIndex = function (params) {
        $.extend(this.params, params);
        this._init();
    };

    //原型
    userIndex.prototype = {

        params : {

        }

        ,_init : function () {
            var that = this;
            that.get_user_info();       //step1:获取用户详情接口
            that.getCompanyInfo();      //step2:获取公司详情用于显示申请单、报销单和补助单
            that.get_my_trip_policy();  //step3:获取我的差旅政策
            that.travelPolicy();        //step4:差旅政策弹出
            that.avatarListen();        //step5:头像监听的操作
            that.pushDown();            //step6:下拉刷新的操作
            that.customerCall();        //step7:客服电话
            that.openAllWindow();       //step8:各个操作打开新窗口
        }

        //step1:获取用户详情接口
        ,get_user_info : function () {
            var param = {};
            param.url = Config.user.userDetail;
            param.data = {};
            ajax(param, function(ret) {
                if (ret.code === 1) {
                    var res = ret.data;
                    var avatar = res.avatar;
                    var user_name = res.user_name;
                    var company_name = res.company_name;
                    if (avatar.length === 0) {
                        avatar = "../../image/head_portrait.png";
                    }
                    $('#avatar').attr('src', avatar);
                    $('#user_name').text(user_name);
                    $('#company_name').text(company_name);
                }
            })
        }

        //step2:获取公司详情用于显示申请单、报销单和补助单
        ,getCompanyInfo : function () {
            var param = {};
            param.url = Config.common.getCompanyInfo;
            param.data = {};
            ajax(param, function(ret) {
                //console.log(JSON.stringify(ret));
                //申请单
                if (ret.apply_order === 1) {
                    $('.travel_apply_expense').show();
                    $('.apply_order').show();
                }
                //报销单和补助单
                if (ret.expense_order === 1) {
                    $('.travel_apply_expense').show();
                    $('.expense_order').show();
                }
            })
        }

        //step3:获取我的差旅政策
        ,get_my_trip_policy : function () {
            var param = {};
            param.url = Config.common.myTripPolicy;
            param.data = {};
            ajax(param, function(ret) {
                var dataInter = ret.data;
                var interText = doT.template($("#traveltpl").text());
                $("#policys").html(interText(dataInter));
            });
        }

        //step4:差旅政策弹出
        ,travelPolicy : function () {
            $('#travelPolicy').click(function () {
                var dialog = new auiDialog({});
                var remarkPolicy = $('#policys').html();
                dialog.alert({
                    msg: remarkPolicy,
                    buttons: ["确定"]
                }, function(ret) {
                    console.log(JSON.stringify(ret));
                })
            });
        }

        //step5:头像监听的操作
        ,avatarListen : function () {
            var that = this;
            api.addEventListener({
                name: 'headerTop'
            }, function(ret, err) {
                if (ret.value.is_image === 1) {
                    that.get_user_info();
                }
            });
        }

        //step6:下拉刷新的操作
        ,pushDown : function () {
            var that = this;
            push_down(function() {
                that.get_user_info();           //step1:获取用户详情接口
                that.getCompanyInfo();          //step2:获取公司详情用于显示申请单、报销单和补助单
                that.get_my_trip_policy();      //step3:获取我的差旅政策
                api.refreshHeaderLoadDone();
            });
        }

        //step7:客服电话
        ,customerCall : function () {
            $('.icon-kefu').click(function () {
                api.call({
                    type: 'tel_prompt',
                    number: '0351-8222211'
                });
            });
        }

        //step8:各个操作打开新窗口
        ,openAllWindow : function () {
            //step1:我的订单
            $('#my_order').click(function () {
                api.openWin({
                    name: 'order_index_win',
                    url: './order/list_win.html'
                });
            });

            //step2:常用联系人
            $('#contact').click(function () {
                openWin('index_win', './contact/index_win.html');
            });

            //step3:常用出行人
            $('#frequenter').click(function () {
                openWin('frequenter_index', './frequenter/index_win.html');
            });

            //step4:出差申请单
            $('#travel_applay').click(function () {
                openWin('travel_apply_order', '../index/apply/travel_apply_order_win.html');
            });

            //step5:出差报销单
            $('#travel_expense').click(function () {
                openWin('travel_expense_order', '../index/expense/travel_expense_order_win.html');
            });

            //step6:出差补助单
            $('#travel_allowance').click(function () {
                openWin('travel_allowance_order', '../index/allowance/travel_allowance_order_win.html');
            });

            //step7:个人信息
            $('#personal_details').click(function () {
                openWin('details', './details_win.html');
            });

            //step8:修改密码
            $('#modify_password').click(function () {
                openWin('modify_password', './modify_password_win.html');
            });

            //step9:修改支付密码
            $('#modify_pay_password').click(function () {
                var param = {};
                param.url = Config.common.isExistsPayPwd;
                param.data = {};
                ajax(param, function(ret) {
                    if (ret.code === 1) {
                        api.openWin({
                            name: 'modify_pay_password',
                            url: './modify_pay_password_win.html',
                            pageParam: {
                                isExists: ret.data.is_exists
                            }
                        });
                    }
                });
            });

            //step10:修改支付密码
            $('#setting').click(function () {
                openWin('setting_win', './system/index_win.html');
            });
        }
    };

    window.userIndex = userIndex;
})(window);
